package com.bbz.core.uuid;

import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

public class UuidUtils {
	private static int start = 10000000;
	private static AtomicInteger counter = new AtomicInteger(start);
	private static String preStr = "T";
	private static String[] chars = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F",
			"G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

	public static String caseNoGenerate() {
		StringBuffer preBuf = new StringBuffer("");
		preBuf.append(preStr);
		String[] result = generateCodeValue(1);
		preBuf.append(result[0]);
		return preBuf.toString();
	}

	/**
	 * 
	 * @author: binbin.zhang
	 * @history:
	 * @param size
	 * @return String[]
	 */
	private static String[] generateCodeValue(Integer size) {
		String[] result = new String[size];
		StringBuffer sb = new StringBuffer("");
		for (int j = 0; j < size; j++) {
			UUID uuid = UUID.randomUUID();
			for (int i = 0; i < 8; i++) {
				String str = uuid.toString().replace("-", "").substring(i * 4, i * 4 + 4);
				int tmp = Integer.parseInt(str, 16); // UUID 是16位编码的
				sb.append(chars[tmp % 36]);
			}
			int t = getNextInt(99999999);
			for (int k = 0; k < 2; k++) {
				String str = String.valueOf(t).substring(k * 4, k * 4 + 4);
				int tmp = Integer.parseInt(str);
				sb.append(chars[tmp % 36]);
			}
			result[j] = sb.toString();
			sb.setLength(0);
		}
		return result;
	}

	/**
	 * 
	 * @author: binbin.zhang
	 * @history:
	 * @param maxid
	 * @return long
	 */
	public static synchronized int getNextInt(int maxid) {
		if (counter.get() > maxid) {
			counter.set(start + 1);
		}
		return counter.incrementAndGet();
	}

	public static void main(String[] arg) {

		for(int i=0;i<10000;i++) {
			System.out.println(caseNoGenerate());
			System.out.println(UUID.randomUUID().toString());
		}
	}
}
